package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.qf.feign.SearchFeign;
import com.qf.pojo.Customer;
import com.qf.service.CacheServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试 远程调用的查询缓存
 * @author lixu
 */
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private SearchFeign searchFeign;

    @Autowired
    private CacheServiceImpl cacheService;

    /**
     * 缓存搜索微服务的 结果  Customer对象
     * Map  map
     * @HystrixCommand(commandKey = "customerList") : key
     * @CacheKey  Integer id    : key的一部分
     * @CacheResult: value
     * map.put(key
     */
    @GetMapping("/list/{id}")
    public Customer list(@PathVariable Integer id){
        Customer customer = cacheService.findById(id);//调用

        Customer customer2 = cacheService.findById(id);//缓存

        //添加或修改或删除  被调用方
        //走一波清除
        cacheService.clearFindById(id);

        Customer customer3 = cacheService.findById(id);//调用 --被调用方

        return customer;
    }

}
